<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Model for the shifts
 *
 * @category 	RedCross
 * @package		Core_Model
 */
class Core_Model_Shift extends RedCross_Gateway_Base_Shift implements Zend_Acl_Resource_Interface
{
	/**
	 * State constant. The initial state of a shift.
	 * A shift in this state has no members assigned to its roles.
	 * @var int
	 */
	const STATE_CREATED = 1;

	/**
	 * State constant. The second state of a shift.
	 * A shift in this state has at least one member assigned to one of its roles.
	 * @var int
	 */
	const STATE_VALID = 2;

	/**
	 * State constant. The third state of a shift
	 * To be in this state, all members registered on it have received a shift notification
	 * @var int
	 */
	const STATE_NOTIFICATIONS_SENT = 3;

	/**
	 * State constant. The forth state of a shift.
	 * To be in this state, all members registered on it must have received a shift notification
	 * and confirmed it.
	 * @var int
	 */
	const STATE_NOTIFICATIONS_CONFIRMED = 4;

	/**
	 * State constant. The fifth state of a shift.
	 * A shift is in this state when its start date is larger than today's date
	 * @var int
	 */
	const STATE_COMPLETE = 5;

	/**
	 * State constant. The sixth state of a shift.
	 * A shift is in this state when the shift report has been submitted
	 * @var int
	 */
	const STATE_REPORT_SUBMITTED = 6;

	/**
	 * State constant. The last state of a shift.
	 * A shift is in this state when a shift report has been submitted to it and the
	 * report has been approved.
	 * @var int
	 */
	const STATE_REPORT_APPROVED = 7;


	private $STATES = array (
		self::STATE_CREATED 				=>		'Opprettet',
		self::STATE_VALID					=>		'Gyldig',
		self::STATE_NOTIFICATIONS_SENT		=>		'Utkalling sendt til alle',
		self::STATE_NOTIFICATIONS_CONFIRMED =>		'Utkalling bekreftet av alle',
		self::STATE_COMPLETE				=>		'Utf&oering;rt',
		self::STATE_REPORT_SUBMITTED		=>		'Rapport levert',
		self::STATE_REPORT_APPROVED			=>		'Rapport godkjent'
	);
	/**
	 * Returns the roles the member has on this shift
	 * @param int $member_id
	 * @return array Of Core_Model_ShiftRole
	 */
	public function getMemberRoles($member_id)
	{
		if (count($this->Roles) > 0)
		{
			$memberRoles = array();
			foreach ($this->Roles as $role)
			{
				if ((int)$role->member_id == (int)$member_id)
				{
					$memberRoles[] = $role;
				}
			}
			return $memberRoles;
		} else
		{
			return false;
		}
	}

	public function getState()
	{
		$date = new Zend_Date();
		if ($this->from_date < $date->get('YYYY-MM-dd HH:mm:ss'))
		{
			//Now we have can only be in state from completed and out
			if (!isset($this->ShiftReport))
			{
				//If no report, there's only one option: complete
				return self::STATE_COMPLETE;
			} else
			{
				if ((bool)$this->ShiftReport->approved)
				{
					return self::STATE_REPORT_APPROVED;
				} else
				{
					return self::STATE_REPORT_SUBMITTED;
				}
			}
		} else
		{
			//If this is a future shift
			if (count($this->Roles) == 0)
			{
				//And it has no filled roles
				return self::STATE_CREATED;
			} else
			{
				$canBe_NOTIFICATIONS_SENT = true;		//true until we find a shiftrole where a shift notification is not sent
				$canBe_NOTIFICATIONS_CONFIRMED = true;	//true until we find a notification that is not confirmed
				$isVALID = false;						//false until we find one filled shiftrole
				foreach ($this->Roles as $role)
				{
					if (is_null($role->notification_sent_time))
					{
						$canBe_NOTIFICATIONS_SENT = false;
					}
					if (is_null($role->notification_confirmed_time))
					{
						$canBe_NOTIFICATIONS_CONFIRMED = false;
					}
					if (!is_null($role->member_id))
					{
						$isVALID = true;
					}
					if (!$canBe_NOTIFICATIONS_SENT && !$canBe_NOTIFICATIONS_CONFIRMED && $isVALID)
					{
						return self::STATE_VALID;
					}
				}
				if ($canBe_NOTIFICATIONS_CONFIRMED)
				{
					return self::STATE_NOTIFICATIONS_CONFIRMED;
				}
				if ($canBe_NOTIFICATIONS_SENT)
				{
					return self::STATE_NOTIFICATIONS_SENT;
				}
				if ($isVALID)
				{
					return self::STATE_VALID;
				}
				return self::STATE_CREATED;
			}

		}

	}

    public function getResourceId ()
    {
        return 'model:shift';
    }
}